/*
* Copyright 2012-2019 the original author or authors.
*
* Generate by Spring Data MyBatis (https://github.com/hatunet/spring-data-mybatis).
*/
{{#entity}}
package {{packageName}};

import static com.querydsl.core.types.PathMetadataFactory.*;
import static com.querydsl.core.types.Projections.*;

import java.sql.Types;
import java.util.*;

import com.querydsl.core.types.*;
import com.querydsl.core.types.dsl.*;
import com.querydsl.sql.ColumnMetadata;

public class {{queryClassName}} extends org.springframework.data.mybatis.querydsl.MybatisRelationalPathBase<{{queryClassName}}, {{className}}> {

    private static final long serialVersionUID = {{serialVersion}}L;

    public static final {{queryClassName}} {{instanceName}} = new {{queryClassName}}("{{instanceName}}");

    public {{queryClassName}}(String variable) {
        super({{queryClassName}}.class, forVariable(variable), {{#hasSchema}}"{{schema}}"{{/hasSchema}}{{^hasSchema}}null{{/hasSchema}}, "{{tableName}}");
        addMetadata();
    }

    public {{queryClassName}}(String variable, String schema, String table) {
        super({{queryClassName}}.class, forVariable(variable), schema, table);
        addMetadata();
    }

    public {{queryClassName}}(String variable, String schema) {
        super({{queryClassName}}.class, forVariable(variable), schema, "{{tableName}}");
        addMetadata();
    }

    public {{queryClassName}}(Path<? extends {{queryClassName}}> path) {
        super(path.getType(), path.getMetadata(), {{#hasSchema}}"{{schema}}"{{/hasSchema}}{{^hasSchema}}null{{/hasSchema}}, "{{tableName}}");
        addMetadata();
    }

    public {{queryClassName}}(PathMetadata metadata) {
        super({{queryClassName}}.class, metadata, {{#hasSchema}}"{{table.schema}}"{{/hasSchema}}{{^hasSchema}}null{{/hasSchema}}, "{{tableName}}");
        addMetadata();
    }

    {{#properties}}
    {{#path}}{{^embedded}}
    public final {{pathType}}{{#needPattern}}<{{javaType}}>{{/needPattern}} {{name}} = create{{pathFactory}}("{{name}}"{{#needPattern}}, {{javaType}}.class{{/needPattern}});
    {{/embedded}}{{/path}}
    {{#embedded}}
    public final {{queryJavaType}} {{name}} = new {{queryJavaType}}("{{instanceName}}");
    {{/embedded}}
    {{#toOne}}
    {{#association}}
    {{#locals}}
    public final {{pathType}}{{#needPattern}}<{{javaType}}>{{/needPattern}} {{name}} = create{{pathFactory}}("{{name}}"{{#needPattern}}, {{javaType}}.class{{/needPattern}});
    {{/locals}}
    public final com.querydsl.sql.ForeignKey<{{property.queryJavaType}}> {{property.name}}FK = createForeignKey(Arrays.asList({{#locals}}{{^-first}}, {{/-first}}{{name}}{{/locals}}),Arrays.asList({{#foreigns}}{{^-first}}, {{/-first}}"{{columnName}}"{{/foreigns}}));
    public final {{property.queryJavaType}} {{property.name}} = new {{property.queryJavaType}}("{{property.name}}");
    {{/association}}
    {{/toOne}}
    {{/properties}}

    public void addMetadata() {
    {{#properties}}
    {{#path}}
        addMetadata({{name}}, ColumnMetadata.named("{{columnName}}").withIndex({{-index}}).ofType(Types.{{jdbcType}}));
    {{/path}}
    {{/properties}}
    }

    @Override
    protected Class<{{className}}> domainClass() {
        return {{className}}.class;
    }

    @Override
    public FactoryExpression<{{className}}> projections() {
        List<Expression<?>> paths = new LinkedList<>();
        paths.addAll(Arrays.asList(all()));
        {{#properties}}
        {{#embedded}}
        paths.add(bean({{javaType}}.class, {{name}}.all()).as("{{name}}"));
        {{/embedded}}
        {{#toOne}}
            {{#association}}
        paths.add(bean({{property.javaType}}.class, {{property.name}}.all()).as("{{property.name}}"));
            {{/association}}
        {{/toOne}}
        {{/properties}}
        Expression[] expressions = paths.toArray(new Expression[paths.size()]);
        return bean({{className}}.class, expressions);
    }

    @Override
    public org.springframework.data.mybatis.querydsl.MybatisSQLQuery<{{className}}> handleQuery(org.springframework.data.mybatis.querydsl.MybatisSQLQuery<{{className}}> query) {
        return query
    {{#properties}}
        {{#toOne}}
            {{#association}}
        .leftJoin({{property.name}}FK, {{property.name}})
            {{/association}}
        {{/toOne}}
    {{/properties}}
        ;
    }

}
{{/entity}}
